package net.mhelvens.deltaprofiles;

import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;

public class LogicalOrCondition extends BaseLogicalCondition {
	@SuppressWarnings("unused") private static final String TAG = LogicalOrCondition.class.getSimpleName();
	
	public static Uri static_uri() {
		return new Uri.Builder().path(
				LogicalOrCondition.class.getName()
		).build();
	}
	
	@Override
	protected boolean eval() {
		boolean result = false;
		for (Condition c: allSubconditions())
			result |= c.eval();
		return result;
	}
	
	private View orLabel() {
		return getLabelView(R.string.logical_or, R.dimen.binary_op_padding, R.dimen.binary_op_padding);
	}
	
	private View orPotentialLabel() {
		return getPotentialLabelView(R.string.logical_or, R.dimen.binary_op_padding, R.dimen.binary_op_padding);
	}
	
	@Override
	public void buildFormula(OnChanged oc, FormulaViewAdapter fva, ViewGroup container) {
		fva.appendView(getLabelView("(", 0, R.dimen.parenthesis_padding));
		
		IterableSparseArray<Condition> subs = allSubconditions();
		
		switch (subs.size()) {
			case 0: {
				fva.appendView(dropTarget(oc, 0));
				fva.appendView(orLabel());
				fva.appendView(dropTarget(oc, 1));
			} break;
			case 1: {
				if (subs.get(0, null) != null) {
					subs.get(0).buildFormula(oc, fva, container);
					fva.appendView(orLabel());
					fva.appendView(dropTarget(oc, 1));
				} else {
					fva.appendView(dropTarget(oc, 0));
					fva.appendView(orLabel());
					subs.get(1).buildFormula(oc, fva, container);
				}
			} break;
			default: {
				subs.get(0).buildFormula(oc, fva, container);
				for (int i = 1; i < subs.size(); ++i) {
					fva.appendView(orLabel());
					subs.get(i).buildFormula(oc, fva, container);
				}
				fva.appendEditView(orPotentialLabel());
				fva.appendEditView(dropTarget(oc, subs.size()));
			} break;
		}
		
		fva.appendView(getLabelView(")", R.dimen.parenthesis_padding, 0));
	}
	
}
